package zhoufu.sz.sanxiao_check.presents;

import javax.inject.Inject;
import rx.Subscription;
import rx.functions.Action1;
import zhoufu.sz.sanxiao_check.event.Event;
import zhoufu.sz.sanxiao_check.event.RxBus;
import zhoufu.sz.sanxiao_check.models.StreetListModel;
import zhoufu.sz.sanxiao_check.net.RequestHelper;
import zhoufu.sz.sanxiao_check.net.RequestRepository;
import zhoufu.sz.sanxiao_check.ui.contract.StreetListContract;

/**
 * User:RCP
 * email:ruancaipu@gmail.com
 * Date:2016/7/21
 */
public class StreetPresenter implements StreetListContract.Presenter
{
    private RequestRepository repository;

    private StreetListContract.View view;

    private int pageIndex = 1;

    private int totalSize;

    private String District;

    private int Status;

    @Inject
    public StreetPresenter(RequestRepository repository)
    {
        this.repository = repository;
        Bus();
    }

    public void bindView(StreetListContract.View view)
    {
        this.view = view;
        District = view.District();
        Status = view.Status();
    }

    @Override public void onStart(Class c)
    {

    }

    @Override public void next()
    {
        pageIndex++;
        onStart();
    }


    private void Bus()
    {
        Subscription subscription2 = RxBus.getDefault()
                .toObserverable(Event.RefreshStreetStatuEvent.class).subscribe(
                        new Action1<Event.RefreshStreetStatuEvent>()
                        {
                            @Override public void call(
                                    Event.RefreshStreetStatuEvent refreshStreetStatuEvent)
                            {
                                view.StreetStatuEvent(refreshStreetStatuEvent);
                            }
                        });
        Subscription subscription3 = RxBus.getDefault()
                .toObserverable(Event.PlaceChangeRefresh.class).subscribe(
                        new Action1<Event.PlaceChangeRefresh>()
                        {
                            @Override public void call(Event.PlaceChangeRefresh placeChangeRefresh)
                            {
                                view.PlaceChangeRefresh(placeChangeRefresh);
                            }
                        });
        repository.addSubscribe(subscription2);
        repository.addSubscribe(subscription3);
    }

    @Override public void onStart()
    {
        repository.GetStreetByPagerNew(Status, view.Grade(), view.Name(), District,
                view.Address(),
                view.Place_No(), view.Building_No(), pageIndex,
                new RequestHelper.RequestCallBack<StreetListModel>()
                {
                    @Override public void onPullStart()
                    {
                        if ( pageIndex == 1 )
                        {
                            view.vStart();
                        }
                    }

                    @Override public void onPullFailud(Throwable throwable)
                    {
                        if ( pageIndex == 1 )
                        {
                            view.pullFaild();
                        }
                    }

                    @Override public void onPullEnd()
                    {
                        view.completedLoadMore();
                    }

                    @Override public void onPullSuccessed(StreetListModel s)
                    {
                        view.completedLoadMore();
                        totalSize = s.getPageCount();
                        if ( s.getErrNum().equals("0") )
                        {
                            if ( s.getData().size() == 0 && pageIndex == 1 )
                            {
                                view.showEmptyView();
                            } else
                            {
                                view.setAdapterData(s.getData(), pageIndex);
                                view.pullEnd();
                            }
                        } else
                        {
                            view.pullFaild();
                            view.showMessage(s.getErrMsg());
                        }
                    }
                });
    }

    @Override public void onPause()
    {

    }

    @Override public void onDestroy()
    {
        repository.onDestory();
    }

    public void setPageFirst()
    {
        pageIndex = 1;
    }


    public int getTotalSize()
    {
        return totalSize;
    }

    public void setDistrict(String district)
    {
        District = district;
    }

    public void setStatus(int status)
    {
        Status = status;
    }
}
